<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :addTodo="addTodo" />
      <List
        :todoList="todoList"
        :singleChange="singleChange"
        :singleDelete="singleDelete"
      />
      <Footer :todoList="todoList" :changeAllDone="changeAllDone" />
    </div>
  </div>
</template>

<script>
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import List from "@/components/List";

export default {
  name: "App",
  components: {
    Header,
    Footer,
    List,
  },
  data() {
    return {
      todoList: [
        { id: 1, thing: "吃", done: false },
        { id: 2, thing: "喝", done: true },
        { id: 3, thing: "打牌", done: false },
      ],
    };
  },
  methods: {
    //1. 子组件修改单个多选框(接受参数是下标的版本)
    singleChange(index) {
      this.todoList[index].done = !this.todoList[index].done;
    },
    //1.1 子组件修改单个多选框(接受参数是id的版本)
    /* singleChange(id) {
      //首先通过find方法找到id匹配的数据
      const data = this.todoList.find((item) => item.id === id);
      data.done = !data.done;
    }, */

    //2. 子组件删除单个(使用splice删除,需要的是下标)
    singleDelete(index) {
      this.todoList.splice(index, 1);
    },

    //3. header组件中新增逻辑
    addTodo(value) {
      const newTodo = { id: Date.now(), thing: value, done: false };

      this.todoList.unshift(newTodo);
    },
    //4. footer组件修改所有状态
    changeAllDone(newDone) {
      this.todoList.forEach((item) => (item.done = newDone));
    },
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
